package com.bustle.facade.proxy;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.bustle.order.dto.account.AccountDto;
import com.bustle.order.dto.account.AccountDtoDao;
import com.bustle.user.AuthenticationException;
import com.bustle.user.dto.user.UserDto;
import com.bustle.user.dto.user.UserDtoMapper;
import com.bustle.user.webservice.AuthenticationDtoWebService;

@Local
public interface AuthenticationDtoManagerProxy
{
  public UserDto authenticate(String aLogin, String aPassword) throws AuthenticationException;
  public void createUser(UserDto aUser, String aLogin, String aPassword) throws AuthenticationException;

  public UserDto getUser(int anId);
  public void updateUser(UserDto aUser);

  @Stateless
  public class DefaultAuthenticationDtoManagerProxy implements AuthenticationDtoManagerProxy
  {
    @PostConstruct
    public void postBuild()
    {
      try
      {
        QName SERVICE_NAME = new QName("http://webservice.user.bustle.com/", "AuthenticationDtoWebService$DefaultAuthenticationDtoWebServiceService");
        QName PORT_NAME = new QName("http://webservice.user.bustle.com/", "AuthenticationDtoWebService$DefaultAuthenticationDtoWebServicePort");
        Service service = Service.create(new URL("http://localhost:8080/bustle-soa-user-0.0.2-SNAPSHOT/DefaultAuthenticationDtoWebService?wsdl"), SERVICE_NAME);
        authenticationDtoManager = service.getPort(PORT_NAME, AuthenticationDtoWebService.class);

      }
      catch (MalformedURLException anException)
      {
        anException.printStackTrace();
      }
    }

    @Override
    public UserDto authenticate(String aLogin, String aPassword) throws AuthenticationException
    {
      try
      {
        return authenticationDtoManager.authenticate(aLogin, aPassword);
      }
      catch (Exception anException)
      {
        throw new AuthenticationException("Unable to authenticate by web service", anException);
      }
    }

    @Override
    public void createUser(UserDto aUser, String aLogin, String aPassword) throws AuthenticationException
    {
      try
      {
        UserDto newUser = authenticationDtoManager.createUser(aUser, aLogin, aPassword);
        userDtoMapper.map(newUser, aUser);

        // Not enough time to develop account webservice
        AccountDto accountDto = new AccountDto();
        accountDto.setCreationDate(new Date());
        accountDto.setUser(aUser);
        accountDtoDao.create(accountDto);

      }
      catch (Exception anException)
      {
        throw new AuthenticationException("Unable to create user by web service", anException);
      }
    }

    @Override
    public UserDto getUser(int anId)
    {
      return authenticationDtoManager.getUser(anId);
    }

    @Override
    public void updateUser(UserDto aUser)
    {
      authenticationDtoManager.updateUser(aUser);
    }

    protected AuthenticationDtoWebService authenticationDtoManager;
    @EJB
    protected UserDtoMapper userDtoMapper;
    @EJB
    protected AccountDtoDao accountDtoDao;
  }
}
